package tom.change.swing.index;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.json.JSONArray;
import org.json.JSONObject;

import tom.change.pojo.User;
import tom.change.util.CircleProgressBar;
import tom.change.util.Constant;
import tom.change.util.Method;
import tom.change.util.MyJpanel;
import tom.change.util.Sleep;

import com.baidu.aip.face.AipFace;

public class Index extends JFrame implements MouseListener {

	/**
     * 
     */
	private static final long serialVersionUID = 1L;
	private String path="";
	private JPanel panel;
	private JPanel panel_Left;
	private JPanel panel_right;
	private JPanel panel_bottom;
	private JPanel right_down;
	private JFileChooser file;
	private JButton searchButton;
	private JButton checkButton;
	// 主界面布局
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	// 中间界面布局
	private GridBagLayout _layout;
	private GridBagConstraints _constraints;
	// 文件选择框
	private JFileChooser fileChooser;
	private JTextField fileTextField;
	private JButton chooseButton;
	private JButton startSearchbButton;
	private JFileChooser fileChooser2;
	private JTextField fileTextField2;
	private JButton chooseButton2;
	private JButton startCompareButton;
	// 功能列表按钮
	private JButton button1;
	private JButton button2;
	private JButton button3;
	private JButton button4;
	private JButton buttonbottom;
	// 脸部类
	private AipFace client;
	private HashMap<String, String> ops;

	// double数据处理格式
	DecimalFormat df;
	//绘画类别
	private Method type;
	
	private int searchCount=1;
	
	//图片显示  人脸检测
	private JPanel panel2;
	//图片显示  人脸检测
	private JPanel panel2Compare1;
	private JPanel panel2Compare2;
	//图片显示  人脸检测
	private JPanel panel2Register;
	//图片显示  人脸检测
	private JPanel panel2Search;
	private CircleProgressBar circleProgressBar1;
	public Index() {
		initComponent();
		initJframe();
		initEvent();
		// 注册一个智能小手
		client = new AipFace(Constant.APP_ID, Constant.API_KEY,
				Constant.SECRET_KEY);
		ops = new HashMap<String, String>();
		df = new DecimalFormat("######0.00");
	}

	private void initComponent() {
		searchButton = new JButton("欢迎使用彭清瑶人脸识别系统！！如有不足请指出");

		this.layout = new GridBagLayout();

		this.constraints = new GridBagConstraints();

		this.constraints.fill = 1;

		this.panel_Left = new JPanel(){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon("img\\1.jpg");  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),(int) screensize.getHeight() - 40, icon.getImageObserver());  
            } 
		};
		//this.panel_Left.setBackground(Color.blue);
		this.panel_right = new JPanel();

		this.panel_right.setLayout(new BorderLayout());
		this.panel_right.add(searchButton, BorderLayout.NORTH);
		right_down = new JPanel();
		//right_down.setBackground(Color.yellow);
		initCenterPanel(right_down);
		this.panel_right.add(right_down, BorderLayout.CENTER);
		// this.table.setBackground(Color.red);
		this.panel_bottom = new JPanel();
		initCenterPanelBottom(this.panel_bottom);
		panelSets();
		//panel_bottom.setBackground(Color.black);
		// this.panel_bottom.setBackground(Color.red);
		this.panel = new JPanel();
		makeComponent(this.panel_Left, 1, 6, 1.0, 1.0);
		makeComponent(this.panel_right, 0, 5, 5.2, 1.0);
		makeComponent(this.panel_bottom, 0, 1, 5.2, 0.2);

		this.panel.setLayout(this.layout);
		add(this.panel);
	}
	
	public void panelSets(){
	    right_down.setBorder(BorderFactory.createLineBorder(Color.gray));
	    panel_Left.setBorder(BorderFactory.createLineBorder(Color.gray));
	    panel_bottom.setBorder(BorderFactory.createLineBorder(Color.gray));
	    
	}
	private void initJframe() {
		setTitle("人脸识别首页");

		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();

		int my_width = (int) screensize.getWidth() - 200;
		int my_heigth = (int) screensize.getHeight() - 40;

		setBounds(((int) screensize.getWidth() - my_width) / 2,
				((int) screensize.getHeight() - my_heigth) / 2, my_width,
				my_heigth);
		setDefaultCloseOperation(3);
		setVisible(true);
	}

	private void initEvent() {
		this.searchButton.addMouseListener(this);
		this.panel_bottom.addMouseListener(this);
		this.button1.addMouseListener(this);
		this.button2.addMouseListener(this);
		this.button3.addMouseListener(this);
		this.button4.addMouseListener(this);
	}

	
	private void initCenterPanelBottom(JPanel panel) {
		buttonbottom = new JButton("退出");
		buttonbottom.addMouseListener(this);
		if (_layout == null) {
			_layout = new GridBagLayout();
		}
		if (_constraints == null) {
			_constraints = new GridBagConstraints();
		}
		_constraints.fill = 1;
		// 设置用这个约束的间距 上左下右
		_constraints.insets = new Insets(6, 20, 6, 20);
		makeComponent(panel, _layout, _constraints, buttonbottom, 0, 1, 0.5, 1);
		panel.setLayout(_layout);
	}
	// 给中间窗口加入布局
	private void initCenterPanel(JPanel panel) {
		button1 = new JButton("人脸度检测");
		button2 = new JButton("人脸对比");
		button3 = new JButton("人脸注册");
		button4 = new JButton("人脸查找");
		if (_layout == null) {
			_layout = new GridBagLayout();
		}
		if (_constraints == null) {
			_constraints = new GridBagConstraints();
		}
		_constraints.fill = 1;
		// 设置用这个约束的间距 上左下右
		_constraints.insets = new Insets(6, 20, 6, 20);
		makeComponent(panel, _layout, _constraints, button1, 0, 1, 0.5, 1);
		makeComponent(panel, _layout, _constraints, button2, 0, 1, 0.5, 1);
		makeComponent(panel, _layout, _constraints, button3, 0, 1, 0.5, 1);
		makeComponent(panel, _layout, _constraints, button4, 0, 1, 0.5, 1);
		panel.setLayout(_layout);
	}

	// 在jpanel中画文件选择的界面
	public void draw(JPanel panel) {
		type=Method.normal;
		if (_layout == null) {
			_layout = new GridBagLayout();
		}
		if (_constraints == null) {
			_constraints = new GridBagConstraints();
		}
		_constraints.fill = 1;
		// 设置用这个约束的间距 上左下右
//		_constraints.insets = new Insets(6, 20, 6, 20);
		_constraints.ipady=9;
		fileTextField = new JTextField();
		chooseButton = new JButton("选择照片");
		chooseButton.addMouseListener(this);
		startSearchbButton = new JButton("开始检测");
		startSearchbButton.addMouseListener(this);
		file = new JFileChooser();
		panel2=new JPanel(){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon("img\\1s.jpg");  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),icon.getIconHeight(), icon.getImageObserver());  
            } 
		};
		makeComponent(panel, _layout, _constraints, fileTextField, 1, 1, 0.7,
				0.1);
		makeComponent(panel, _layout, _constraints, chooseButton, 0, 1, 0.1,
				0.1);
		makeComponent(panel, _layout, _constraints, startSearchbButton, 0, 2,
				0.8, 0.1);
		makeComponent(panel, _layout, _constraints, panel2, 1, 2,
				0.8, 0.8);
		makeLine(panel, _layout, _constraints, 2);
		panel.setLayout(_layout);
	}

	// 人脸对比界面重绘
	public void drawCompare(JPanel panel) {
		if (_layout == null) {
			_layout = new GridBagLayout();
		}
		if (_constraints == null) {
			_constraints = new GridBagConstraints();
		}
		_constraints.fill = 1;
		// 设置用这个约束的间距 上左下右
		_constraints.insets = new Insets(6, 20, 6, 20);
		fileTextField = new JTextField();
		chooseButton = new JButton("选择照片");
		chooseButton.addMouseListener(this);
		fileTextField2 = new JTextField();
		chooseButton2 = new JButton("选择照片");
		chooseButton2.addMouseListener(this);
		startCompareButton = new JButton("开始对比");
		startCompareButton.addMouseListener(this);
		file = new JFileChooser();
		panel2Compare1=new JPanel(){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon("img\\1s.jpg");  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),icon.getIconHeight(), icon.getImageObserver());  
            } 
		};
		panel2Compare2=new JPanel(){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon("img\\1s.jpg");  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),icon.getIconHeight(), icon.getImageObserver());  
            } 
		};
		makeComponent(panel, _layout, _constraints, fileTextField, 1, 1, 0.7,
				0.1);
		makeComponent(panel, _layout, _constraints, chooseButton, 0, 1, 0.1,
				0.1);
//		makeLine(panel, _layout, _constraints, 1);
		makeComponent(panel, _layout, _constraints, fileTextField2, 1, 1, 0.7,
				0.1);
		makeComponent(panel, _layout, _constraints, chooseButton2, 0, 1, 0.1,
				0.1);
//		makeLine(panel, _layout, _constraints, 2);
		makeComponent(panel, _layout, _constraints, startCompareButton, 0, 2,
				0.8, 0.1);
		makeComponent(panel, _layout, _constraints, panel2Compare1, 1, 2, 0.4,
				0.8);
		makeComponent(panel, _layout, _constraints, panel2Compare2, 0, 2, 0.4,
				0.8);
		panel.setLayout(_layout);
	}

	// 人脸对比界面重绘
	public void drawRegister(JPanel panel) {
		type=Method.register;
		if (_layout == null) {
			_layout = new GridBagLayout();
		}
		if (_constraints == null) {
			_constraints = new GridBagConstraints();
		}
		_constraints.fill = 1;
		// 设置用这个约束的间距 上左下右
		_constraints.insets = new Insets(6, 20, 6, 20);
		fileTextField = new JTextField();
		fileTextField2 = new JTextField();
		chooseButton = new JButton("选择照片");
		chooseButton.addMouseListener(this);
		startSearchbButton = new JButton("开始注册");
		startSearchbButton.addMouseListener(this);
		file = new JFileChooser();
		panel2Register=new JPanel(){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon("img\\1s.jpg");  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),icon.getIconHeight(), icon.getImageObserver());  
            } 
		};
		makeComponent(panel, _layout, _constraints, fileTextField, 1, 1, 0.7,
				0.1);
		makeComponent(panel, _layout, _constraints, chooseButton, 0, 1, 0.1,
				0.1);
		makeComponent(panel, _layout, _constraints, fileTextField2, 1, 2,
				0.7, 0.3);
		makeComponent(panel, _layout, _constraints, new JLabel("姓名"), 0, 2,
				0.1, 0.3);
		makeComponent(panel, _layout, _constraints, startSearchbButton, 0, 2,
				0.8, 0.3);
		makeComponent(panel, _layout, _constraints, panel2Register, 1, 2,
				0.8, 0.8);
		makeLine(panel, _layout, _constraints, 2);
		panel.setLayout(_layout);
	}

	public void drawSearch(JPanel panel) {
		type=Method.search;
		if (_layout == null) {
			_layout = new GridBagLayout();
		}
		if (_constraints == null) {
			_constraints = new GridBagConstraints();
		}
		_constraints.fill = 1;
		// 设置用这个约束的间距 上左下右
		_constraints.insets = new Insets(6, 20, 6, 20);
		fileTextField = new JTextField();
        chooseButton = new JButton("选择照片");
        chooseButton.addMouseListener(this);
        startSearchbButton = new JButton("开始查找");
        startSearchbButton.addMouseListener(this);
        file = new JFileChooser();
        panel2Search=new JPanel(){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon("img\\1s.jpg");  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),icon.getIconHeight(), icon.getImageObserver());  
            } 
		};
//        panel2Search = reDrawPic(panel2Search, "img\\1s.jpg");
        makeComponent(panel, _layout, _constraints, fileTextField, 1, 1, 0.7,
                0.1);
        makeComponent(panel, _layout, _constraints, chooseButton, 0, 1, 0.1,
                0.1);
        makeComponent(panel, _layout, _constraints, startSearchbButton, 0, 2,
                0.8, 0.1);
        makeComponent(panel, _layout, _constraints, panel2Search, 1, 2,
				0.8, 0.8);
        makeLine(panel, _layout, _constraints, 2);
        panel.setLayout(_layout);
        path="E:\\chirs\\Pictures\\57813aefebd3246cc29f9689e92b7352.jpg";
        panel2Search=new MyJpanel(path){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon(getPath());  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),icon.getIconHeight(), icon.getImageObserver());  
            } 
		};
		panel.updateUI();
		panel2Search.updateUI();
	}

	public JPanel reDrawPic(JPanel panel , String path){
		path="E:\\chirs\\Pictures\\57813aefebd3246cc29f9689e92b7352.jpg";
		System.out.println(path);
		panel=new MyJpanel(path){
		    protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon(getPath());  
                Image img = icon.getImage();  
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
                g.drawImage(img, 0, 0,icon.getIconWidth(),icon.getIconHeight(), icon.getImageObserver());  
            } 
		};
		return panel;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == this.button1) {
			this.right_down.removeAll();
			// 清空后重绘
			draw(right_down);
			initEvent();
			this.right_down.revalidate();
			// this.right_down.repaint();
		}
		if (e.getSource() == this.button2) {
			this.right_down.removeAll();
			// 清空后重绘
			drawCompare(right_down);
			initEvent();
			this.right_down.revalidate();
			// this.right_down.repaint();
		}
		if (e.getSource() == this.button3) {
			this.right_down.removeAll();
			// 清空后重绘
			drawRegister(right_down);
			initEvent();
			this.right_down.revalidate();
			// this.right_down.repaint();
		}
		if (e.getSource() == this.button4) {
			this.right_down.removeAll();
			// 清空后重绘
			drawSearch(right_down);
			initEvent();
			this.right_down.revalidate();
			// this.right_down.repaint();
		}
		if (e.getSource() == this.buttonbottom) {
			if (right_down.getComponentCount() != 2) {
				right_down.removeAll();
				initCenterPanel(right_down);
				initEvent();
				this.right_down.revalidate();
			}
		}
		if (e.getSource() == this.chooseButton) {
			String result = "";
			file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			file.showDialog(new JLabel(), "选择");
			File files = file.getSelectedFile();
			if (files != null) {
				if (files.isDirectory()) {
					result = files.getAbsolutePath();
				} else if (files.isFile()) {
					result = files.getAbsolutePath();
				}
			}
			panel2 = reDrawPic(panel2,result);
			panel2Compare1 = reDrawPic(panel2Compare1,result);
			panel2.revalidate();
			if (!checkFile(result, "jpg,jpeg,png,JPG,JPEG,PNG")) {
				result = "";
				JOptionPane.showMessageDialog(this.fileTextField,
						"暂只支持图片格式(.jpg.jpeg.png)", "格式", 0);
			} else {
				fileTextField.setText(result);
			}
		}
		if (e.getSource() == this.chooseButton2) {
			String result = "";
			file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			file.showDialog(new JLabel(), "选择");
			File files = file.getSelectedFile();
			if (files != null) {
				if (files.isDirectory()) {
					result = files.getAbsolutePath();
				} else if (files.isFile()) {
					result = files.getAbsolutePath();
				}
			}
			panel2Compare2=reDrawPic(panel2Compare2, result);
			if (!checkFile(result, "jpg,jpeg,png,JPG,JPEG,PNG")) {
				result = "";
				JOptionPane.showMessageDialog(this.fileTextField,
						"暂只支持图片格式(.jpg.jpeg.png)", "格式", 0);
			} else {
				fileTextField2.setText(result);
			}
		}
		if (e.getSource() == this.startSearchbButton) {
			if(type==Method.normal){
				startCheck();
			}else if(type==Method.register){
				startRegister();
			}else if(type==Method.search){
				startSearch();
			}
		}
		if (e.getSource() == this.startCompareButton) {
			System.out.println("开始进行对比");
			// 获取两个照片进行比对。
			String imagePath1 = fileTextField.getText();
			String imagePath2 = fileTextField2.getText();
			if(!"".equals(imagePath1)&&!"".equals(imagePath2)){
			    
			}else {
			    JOptionPane.showMessageDialog(this.fileTextField,
	                    "<html>别为难我哦，你没有选择全呢</html>", "比对过程", 0);
			    return ;
            }
			ArrayList<String> list = new ArrayList<String>();
			list.add(imagePath1);
			list.add(imagePath2);
			JSONObject jsonObject = client.match(list);
			System.out.println(jsonObject);
			int num = jsonObject.getInt("result_num");
			if (num == 0) {
				JOptionPane.showMessageDialog(this.fileTextField,
						"您选择的图片未检测到人脸", "检测结果", 0);
			} else {
				JSONArray jsonArray = jsonObject.getJSONArray("results");
				String result = "";
				for (Object object : jsonArray) {
					JSONObject jsonObject2 = new JSONObject(object.toString());
					double double1 = jsonObject2.getDouble("score");
					int i = jsonObject2.getInt("index_i") + 1;
					int j = jsonObject2.getInt("index_j") + 1;
					result += "第" + i + "张和第" + j + "张的相似度："
							+ df.format(double1) + "<br/>";
				}
				JOptionPane.showMessageDialog(this.fileTextField,
						"<html>您选择的图片<br/>" + result + "</html>", "检测结果", 0);
			}
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/**************************** 辅助方法 ****************************/
	
	public void startCheck(){
		String title = "人脸检测";
		String content = "<html>您选择的是:<br><font size=4 face='楷体' color='red'>"
				+ title + "</font><br>确定查找？</html>";
		Object[] options = { "确定！", "容我想想！" };

		int res = JOptionPane.showOptionDialog(null, content, "消息",
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
				null, options, options[0]);
		if (res == 0) {
			String imagePath = fileTextField.getText();
			if("".equals(imagePath)){
			    JOptionPane.showMessageDialog(this.fileTextField,
	                    "<html>别为难我哦，你没有选择呢</html>", "检测过程", 0);
			    return;
			}
			ops.put("max_face_num", "2");
			ops.put("face_fields", "expression");
			JSONObject response = client.detect(imagePath, ops);
			int num = response.getInt("result_num");
			if (num == 0) {
				JOptionPane.showMessageDialog(this.fileTextField,
						"您选择的图片未检测到人脸", "检测结果", 0);
			} else if (num > 0) {
				System.out.println(response);
				JSONArray array = response.getJSONArray("result");
				String result = "";
				for (Object object : array) {
					JSONObject object2 = new JSONObject(object.toString());
					double proba = object2.getDouble("face_probability");
					result += df.format(proba * 100) + "%  <br/>";
				}
				JOptionPane.showMessageDialog(this.fileTextField,
						"<html>您选择的图片检测到" + num + "张人脸概率分别为：<br/>" + result
								+ "</html>", "检测结果", 0);
			}
		}
	}
	
	@SuppressWarnings("deprecation")
	public void startRegister(){
		System.out.println("开始注册");
		String imagePath = fileTextField.getText();
		String imageName = fileTextField2.getText();
		if(!"".equals(imagePath)&&!"".equals(imageName)){
		    
		}else {
		    JOptionPane.showMessageDialog(this.fileTextField,
                    "<html>别为难我哦，你没有选择全呢</html>", "注册", 0);
		    return ;
        }
		ArrayList<String> list = new ArrayList<String>();
		list.add(imagePath);
		Date date = new Date();
		String UID="ID_"+(int)(1+Math.random()*(10-1+1))+"_"+date.getHours();
		JSONObject user = client.getUser(UID);
		if(user.toString().indexOf("error_msg")>-1){
			JSONObject jsonObject = client.addUser(UID, imageName, "group_"+date.getDay()+"_"+date.getHours(), list);
			System.out.println(jsonObject+"jsonObject");
			if(jsonObject.toString().indexOf("error_msg")==-1){
				JOptionPane.showMessageDialog(this.fileTextField,
						"<html>恭喜您！！"+imageName+"注册成功！</html>", "注册结果", 0);
			}else {
				JOptionPane.showMessageDialog(this.fileTextField,
						"<html>网络链接超时！！请重试！！</html>", "注册结果", 0);
			}
		}else {
			JOptionPane.showMessageDialog(this.fileTextField,
					"<html>"+UID+"该id已被注册，请重试！！</html>", "注册结果", 0);
		}
	}
	
	public void startSearch(){
		System.out.println("开始查找用户信息");
		ArrayList<String> list=new ArrayList<String>();
		String imagePath = fileTextField.getText();
		if("".equals(imagePath)){
		    JOptionPane.showMessageDialog(this.fileTextField,
                    "<html>别为难我哦，你没有选择呢</html>", "查询过程", 0);
		    return ;
		}else{
    		JOptionPane.showMessageDialog(this.fileTextField,
                    "<html>时间会比较长，请耐心等待！！</html>", "查询过程", 0);
		}
		list.add(imagePath);
		//取出库里所有用户与当前用户比对
		List<User> users = getAllUsers();
		Map<String, Object> resultMap=new HashMap<String,Object>();
		for (User user : users)
        {
		    List<Map<String,Object>> list2 = veriftyUser(user.getUserId(),list);
		    boolean flag=false;
		    for (Map<String, Object> map : list2)
            {
                if(map.get("result").toString().equals("true")){
                    flag=true;
                    break;
                }
            }
		    if(flag){
		        resultMap.put("user", user);
		        break;
		    }
        }
		//输出结构
		if(resultMap!=null){
		    //有相符的照片
		    User user = (User) resultMap.get("user");
		    JOptionPane.showMessageDialog(this.fileTextField,
                    "<html>恭喜您！！查找成功！该照片在本库中名叫"+user.getUserName()+"</html>", "查找结果", 0);
		}
	}
	
	//获取所有注册的用户
	public List<User> getAllUsers(){
	    List<User> userList=new ArrayList<User>();
	    HashMap<String, Object> options = new HashMap<String, Object>(2);
        options.put("start", 0);
        options.put("num", 1000);
        JSONObject groupList = client.getGroupList(options);
        int num = groupList.getInt("result_num");
        if(num>0){
            JSONArray jsonArray = groupList.getJSONArray("result");
            for (Object group_id : jsonArray)
            {
                 userList.addAll(getUsersFromGroup(group_id.toString()));
            }
        }
        return userList;
	}
	//获取组内用户
	public List<User> getUsersFromGroup(String group_id){
	    Sleep.sleep(2);
	    List<User> list=new ArrayList<User>();
	    HashMap<String, Object> options = new HashMap<String, Object>(2);
        options.put("start", 0);
        options.put("num", 1000);
	    JSONObject users = client.getGroupUsers(group_id,options);
	    int num = users.getInt("result_num");
	    if(num>0){
            JSONArray jsonArray = users.getJSONArray("result");
            for (Object object : jsonArray)
            {
                User user =new User();
                JSONObject jsonUser=new JSONObject(object.toString());
                user.setUserId(jsonUser.getString("uid"));
                user.setUserName(jsonUser.getString("user_info"));
                user.setGroupName(group_id);
                list.add(user);
            }
        }
	    return list;
	}
	//验证图片是否是某个UID的图片
	public List<Map<String, Object>> veriftyUser(String uid , ArrayList<String> path){
	    Sleep.sleep(7);
	    List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
	    HashMap<String, Object> options = new HashMap<String, Object>(1);
	    options.put("top_num", path.size());
	    JSONObject res = client.verifyUser(uid, path, options);
	    int num = res.getInt("result_num");
	    if(num>0){
	        JSONArray jsonArray = res.getJSONArray("results");
	        for (Object object : jsonArray)
            {
	            Map<String, Object> map=new HashMap<String,Object>();
                Double double1=Double.parseDouble(object.toString());
                String format = df.format(double1);
                double1=Double.parseDouble(format);
                map.put("probality", double1);
                if(double1>85){
                    map.put("result", true);
                }else {
                    map.put("result", false);
                }
                list.add(map);
            }
	    }
	    return list;
	}
	
	//圆形
	public void cir(){
	    new Thread() {

            int index = 0;

            @Override
            public void run() {
                while (index <= 100) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("test");
                    circleProgressBar1.setProgress(index++);
                }
            };

        }.start();
	}
	protected void makeComponent(JPanel panel, GridBagLayout layout,
			GridBagConstraints constraints, Component component, int width,
			int height) {
		if (component == null) {
			component = new JLabel(" ");
		}

		constraints.gridwidth = width;
		constraints.gridheight = height;
		layout.addLayoutComponent(component, constraints);
		panel.add(component);
	}

	public void makeComponent(JPanel panel, GridBagLayout layout,
			GridBagConstraints constraints, Component component, int width,
			int height, double weightx, double weighty) {
		constraints.weightx = weightx;
		constraints.weighty = weighty;
		constraints.gridwidth = width;
		constraints.gridheight = height;
		layout.addLayoutComponent(component, constraints);
		panel.add(component);
	}

	public void makeLine(JPanel panel, GridBagLayout layout,
			GridBagConstraints constraints, int time) {
		for (int i = 0; i < time; i++)
			makeComponent(panel, layout, constraints, null, 0, 1);
	}

	protected void makeComponent(Component component, int width, int height) {
		if (component == null) {
			component = new JLabel(" ");
		}

		this.constraints.gridwidth = width;
		this.constraints.gridheight = height;
		this.layout.addLayoutComponent(component, this.constraints);
		this.panel.add(component);
	}

	public void makeComponent(Component component, int width, int height,
			double weightx, double weighty) {
		this.constraints.weightx = weightx;
		this.constraints.weighty = weighty;
		this.constraints.gridwidth = width;
		this.constraints.gridheight = height;
		this.layout.addLayoutComponent(component, this.constraints);
		this.panel.add(component);
	}

	public void makeLine(int time) {
		for (int i = 0; i < time; i++)
			makeComponent(null, 0, 1);
	}

	// 判断文件是否已某种格式结尾的
	public boolean checkFile(String content, String standard) {
		boolean flag = false;
		if (standard.indexOf(",") == -1) {
			if (content.indexOf(standard) > -1) {
				flag = true;
			}
		} else {
			String[] split = standard.split(",");
			for (String string : split) {
				if (content.indexOf(string) > -1) {
					flag = true;
					break;
				}
			}
		}
		if("".equals(content)){
			flag=true;
		}
		return flag;
	}
}
